from django import template
from django.shortcuts import reverse
from django.utils.html import format_html
from xadmin.models import Node, Article
from xadmin.plugins.permission import CheckPerm

from xadmin.plugins import GetUrl, GetTemplate
from xadmin.models import Node


register = template.Library()

@register.filter
def get_node_url(nodeid):
    node_obj = Node.objects.get(id=nodeid)
    if node_obj.force_url:
        return node_obj.force_url
    else:
        template_obj = GetTemplate.get_node_template(node_obj)
        if template_obj:
            router_name = template_obj.router
            if router_name:
                return reverse(router_name, args=(node_obj.id,))

        #router_name = GetUrl.get_node_url(nodeid)
        router_name = node_obj.router

        if router_name:
            return reverse(router_name, args=(node_obj.id,))
        else:
            return '#'


@register.filter
def get_content_url(articleid):
    article = Article.objects.get(id=articleid)
    if article.force_url:
        return article.force_url
    else:
        router_name = GetUrl.get_content_url(article.parent_node_id)
        return reverse(router_name, args=(articleid,))